﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Label1.Visible = false;
        initLabels();
        if (Request["id"] != null)
        {
            this.mylogin(Request["id"],Request["pwd"]);
        }

    }

    private void initLabels()
    {
        this.lbl_loginfirst.Text = Resources.Login_aspx.lbl_loginfirst;
        this.lbl_useremail.Text = Resources.Login_aspx.lbl_useremail;
        this.lbl_password.Text = Resources.Login_aspx.lbl_password;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        this.mylogin(this.TextBox1.Text, this.TextBox2.Text);

    }

    void mylogin(string id, string pwd)
    {
        ctpDataContext db = new ctpDataContext();
        var myuser = from p in db.users
                     where p.email == id && p.pwd == pwd
                     select p;

        if (myuser.Count() != 1)
            this.Label1.Visible = true;
        else
        {
            Session["userno"] = myuser.First().no;

            if (Session["backurl"] != null)
            {
                string backurl = Session["backurl"].ToString();
                Session["backurl"] = null;

                Response.Redirect(backurl);

            }
            else
            {
                Response.Redirect("my/index.aspx?no=" + Session["userno"].ToString());
            }
        }
    }



}
